﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace tsgl
{
    public partial class bibliothecaInfoAdd : Form
    {
        public bibliothecaInfoAdd()
        {
            InitializeComponent();
        }

        private void bibliothecaInfoAdd_Load(object sender, EventArgs e)
        {
            this.cboBookType.DataSource = bibliothecaClass.getBookTypeNameAll();
            this.cboBookType.DisplayMember = "图书类型名称";
            this.cboBookType.SelectedIndex = -1;
            this.cboBookType.Text = "请选择";
            this.cboPlace.DataSource = bibliothecaClass.getDepositary();
            this.cboPlace.DisplayMember = "馆藏地点编号";
            this.cboPlace.SelectedIndex = -1;
            this.cboPlace.Text = "请选择";
            this.btnInstore.Enabled = false;
            this.btnAdd.Enabled = false;
            this.nudNowNumber.Enabled = false;
        }

        public string bookType;
        private void cboBookType_SelectedValueChanged(object sender, EventArgs e)
        {
            DataTable dt = bibliothecaClass.getBookTypeId(cboBookType.Text);
            if (dt.Rows.Count != 0)
            {
                bookType = dt.Rows[0][0].ToString();
            }
        }

        public string publisherISBN;
        private void btnSelectPublisher_Click(object sender, EventArgs e)
        {

            selectPublisher frmSelect = new selectPublisher();
            frmSelect.StartPosition = FormStartPosition.Manual;
            frmSelect.Left = this.txtPublisher.Left + this.Left;
            frmSelect.Top = this.txtPublisher.Top + this.Top + this.txtPublisher.Height + this.Height - this.ClientRectangle.Height;
            frmSelect.ShowDialog();
            this.txtPublisher.Text = frmSelect.getTreeViewSelectNode();
            DataTable dt = bibliothecaClass.getPublisherISBN(this.txtPublisher.Text);
            if(dt.Rows.Count!=0)
            {
             publisherISBN=dt.Rows[0][0].ToString();   
            }
        }

        public int bookPages;
        private void txtBookPages_TextChanged(object sender, EventArgs e)
        {
            if (txtBookPages.Text.Trim() != "")
            {
                try
                {
                    bookPages = int.Parse(this.txtBookPages.Text.Trim());
                }
                catch(Exception)
                {
                    MessageBox.Show("图书页数只能为数字型数据","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    txtBookPages.Text = "";
                    txtBookPages.Focus();
                }
            }

        }

        public float bookPrice;
        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            if (this.txtPrice.Text.Trim() != "")
            {
                try
                {
                    bookPrice = float.Parse(this.txtPrice.Text.Trim());
                }
                catch (Exception)
                {
                    MessageBox.Show("价格只能为数字型数据","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    txtPrice.Text = "";
                    txtPrice.Focus();
                }
            }
        }

        //public static Boolean checkEmpty()
        //{
            //foreach (Control control in Controls)
            //{
            //    if (control is TextBox)//如果该控件是TextBox
            //    {
            //        if (control.Text.Trim() == "") //判断是否为空
            //        {
            //            MessageBox.Show("不能为空！");
            //        }
            //    }
            //}


            //if (sender.Text.Trim() == "")
            //{
            //    MessageBox.Show("输入不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    sender.Focus();
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}

            //if (sender.Text.Trim() == "")
            //{
            //    MessageBox.Show("图书名称不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    sender.Focus();
            //    return false;
            //}
            //else
            //{
            //    return true;
            //}


            //if (sender.Text.Trim() == "请选择")
            //{
            //    MessageBox.Show("请选择图书类型！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    sender.Focus();
            //    return false;
            //}
            //else
            //{
            //    return true;
            //}

            //if (sender.Text.Trim() == "")
            //{
            //    MessageBox.Show("作者不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    sender.Focus();
            //    return false;
            //}
            //else
            //{
            //    return true;
            //}

            //if (sender.Text.Trim() == "")
            //{
            //    MessageBox.Show("出版社不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    sender.Focus();
            //    return false;
            //}
            //else
            //{
            //    return true;
            //}

            //if (sender.Text.Trim() == "")
            //{
            //    MessageBox.Show("图书页数不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    sender.Focus();
            //    return false;
            //}
            //else
            //{
            //    return true;
            //}

            //if (sender.Text.Trim() == "")
            //{
            //    MessageBox.Show("价格不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    sender.Focus();
            //    return false;
            //}
            //else
            //{
            //    return true;
            //}

            //if (sender.Text.Trim() == "")
            //{
            //    MessageBox.Show("图书的ISBN不能为空!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    sender.Focus();
            //    return false;
            //}
            //else
            //{
            //    return true;
            //}

            //if (sender.Text.Trim() == "请选择")
            //{
            //    MessageBox.Show("藏书地点不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    sender.Focus();
            //    return false;
            //}
            //else
            //{
            //    return true;
            //}
        //}

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (txtBibliothecaId.Text.Trim() == "")
             {
                 MessageBox.Show("书目编号不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 txtBibliothecaId.Focus();
             }
             else if (txtBookName.Text.Trim() == "")
             {
                 MessageBox.Show("图书名称不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 txtBookName.Focus();
             }


             else if (cboBookType.Text.Trim() == "请选择")
             {
                 MessageBox.Show("请选择图书类型！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 cboBookType.Focus();
             }

             else if (txtAuthor.Text.Trim() == "")
             {
                 MessageBox.Show("作者不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 txtAuthor.Focus();
             }

             else if (txtPublisher.Text.Trim() == "")
             {
                 MessageBox.Show("出版社不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 txtPublisher.Focus();
             }

             else if (txtBookPages.Text.Trim() == "")
             {
                 MessageBox.Show("图书页数不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 txtBookPages.Focus();
             }

             else if (txtPrice.Text.Trim() == "")
             {
                 MessageBox.Show("价格不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 txtPrice.Focus();
             }

             else if (txtBookISBN.Text.Trim() == "")
             {
                 MessageBox.Show("图书的ISBN不能为空!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 txtBookISBN.Focus();
             }

             else if (cboPlace.Text.Trim() == "请选择")
             {
                 MessageBox.Show("藏书地点不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 cboPlace.Focus();
             }
             else
             {
                DataTable dt = bibliothecaClass.getBibliothecaData(txtBibliothecaId.Text);
                if (dt.Rows.Count == 0)
                {
                    Boolean InsertResult;
                    InsertResult = bibliothecaClass.bibliothecaAdd(txtBibliothecaId.Text.Trim(), txtBookName.Text.Trim(), txtAuthor.Text.Trim(), publisherISBN, txtBookISBN.Text.Trim(), nudPublishDate.Text.Trim(), bookPages, bookPrice, bookType, Convert.ToInt32(nudNowNumber.Value), cboPlace.Text.Trim(), txtSynopsis.Text.Trim(), Convert.ToInt32(nudInNumber.Value));
                    if (InsertResult == true)
                    {

                        btnSave.Enabled = false;
                        if (MessageBox.Show("新增成功，是否要入库？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                        {
                            btnInstore.Enabled = true;
                            btnAdd.Enabled = true;

                        }
                    }
                    else
                    {
                        MessageBox.Show("新增失败，请重试！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtBibliothecaId.Focus();
                        btnSave.Enabled = true;
                    }
                }
                else
                {
                    MessageBox.Show("已存在同一编号的书目，请核对该图书书目");
                    txtBibliothecaId.Text = "";
                    txtBibliothecaId.Focus();
                }
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.txtBibliothecaId.Focus();
            btnInstore.Enabled = false;
            btnAdd.Enabled = false;
        }

        private void nudInNumber_ValueChanged(object sender, EventArgs e)
        {
            if(Convert.ToInt32(nudInNumber.Value)>0)
            {
                this.nudNowNumber.Value = this.nudInNumber.Value;
            }
        }

        private void nudInNumber_Leave(object sender, EventArgs e)
        {
            if (Convert.ToInt32(nudInNumber.Value) > 0)
            {
                this.nudNowNumber.Value = this.nudInNumber.Value;
            }
        }

        private void btnInstore_Click(object sender, EventArgs e)
        {
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            initializationSet();
            txtBibliothecaId.Focus();
            btnSave.Enabled = true;
            btnInstore.Enabled = false;
            btnAdd.Enabled = false;
        }

        private void initializationSet()
        {
            this.txtBibliothecaId.Text = "";
            this.txtBookName.Text = "";
            this.txtAuthor.Text = "";
            this.txtPublisher.Text = "";
            this.txtBookISBN.Text = "";
            this.txtBookPages.Text = "";
            this.txtPrice.Text = "";
            this.cboBookType.SelectedIndex = -1;
            this.cboBookType.Text = "请选择";
            this.nudInNumber.Value = 0;
            this.nudNowNumber.Value = 0;
            this.cboPlace.SelectedIndex = -1;
            this.cboPlace.Text = "请选择";
            this.txtSynopsis.Text = "";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}